package com.dkd.manage.service.Impl;

import com.dkd.common.utils.SecurityUtils;
import com.dkd.manage.constant.MessageConstant;
import com.dkd.manage.constant.PasswordConstant;
import com.dkd.manage.exception.CheckException;
import com.dkd.manage.mapper.PartnerMapper;
import com.dkd.manage.pojo.dto.PartnerQueryListDto;
import com.dkd.manage.pojo.entity.Partner;
import com.dkd.manage.pojo.vo.PartnerVo;
import com.dkd.manage.result.PageResult;
import com.dkd.manage.service.PartnerService;
import com.dkd.manage.utils.RegexUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class PartnerServiceImpl implements PartnerService {


    @Autowired
    private PartnerMapper partnerMapper;


    /**
     * 分页查询合作商列表
     * @param partnerQueryListDto
     * @return
     */
    @Override
    public PageResult<PartnerVo> list(PartnerQueryListDto partnerQueryListDto) {
        //开启分页
        PageHelper.startPage(partnerQueryListDto.getPageNum(), partnerQueryListDto.getPageSize());
        //查询区域列表
        List<PartnerVo> partnerVoList = partnerMapper.list(partnerQueryListDto);
        //创建分页详情对象
        PageInfo<PartnerVo> pageInfo = new PageInfo<>(partnerVoList);
        //封装数据返回
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增合作商
     * @param partner
     */
    @Override
    public Integer add(Partner partner) {
        //判断要添加的手机号是否合法
        if(RegexUtils.isPhoneInvalid(partner.getContactPhone())) {
            throw new CheckException(MessageConstant.PHONE_IS_NOT_CORRECT);
        }
        //对密码进行加密
        partner.setPassword(SecurityUtils.encryptPassword(partner.getPassword()));
        return partnerMapper.add(partner);
    }

    /**
     * 修改合作商
     * @param partner
     * @return
     */
    @Override
    public Integer edit(Partner partner) {
        //判断要修改的手机号是否合法
        if(RegexUtils.isPhoneInvalid(partner.getContactPhone())) {
            throw new CheckException(MessageConstant.PHONE_IS_NOT_CORRECT);
        }
        //对密码进行加密
        partner.setPassword(SecurityUtils.encryptPassword(partner.getPassword()));
        return partnerMapper.edit(partner);
    }

    /**
     * 根据id查询合作商
     * @param id
     * @return
     */
    @Override
    public Partner getPartnerById(Long id) {
        return partnerMapper.getPartnerById(id);
    }

    /**
     * 根据id删除合作商
     * deleteRegionById
     * @param ids
     * @return
     */
    @Override
    public Integer deletePartnerById(Long[] ids) {
        return partnerMapper.deletePartnerById(ids);
    }

    @Override
    public Integer resetPassword(Long id) {
        //根据id查询合作商
        Partner partner = getPartnerById(id);
        //将密码重置为123456并进行加密
        partner.setPassword(SecurityUtils.encryptPassword(PasswordConstant.DEFAULT_PASSWORD));
        //执行修改操作
        return partnerMapper.edit(partner);
    }
}
